package org.csu.mypetstore.controller;

import org.springframework.ui.Model;
import jakarta.servlet.http.HttpSession;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/order")
@SessionAttributes({"order", "account"})
public class OrderController {

    @Autowired
    private OrderService orderService;

    // 显示购物车结算页面
    @GetMapping("/checkout")
    public String checkout(@SessionAttribute Cart cart,HttpSession session, Model model) {
        /*if (cart == null || cart.isEmpty()) {
            return "redirect:/cart/cart";
        }*/
        model.addAttribute("cart", cart);
        return "/cart/checkout"; // 对应checkout.html
    }

    // 显示新订单表单
    @GetMapping("/new")
    public String newOrderForm(
            //@SessionAttribute Account account,
            @SessionAttribute Cart cart,
            HttpSession session,
            Model model) {
        Account account= (Account) session.getAttribute("loginAccount");

        // 账户验证
        if (account == null) {
            return "redirect:/account/signin";
        }

        /*if (cart == null || cart.isEmpty()) {
            return "redirect:/cart/cart";
        }*/

        //Order order = orderService.initializeOrder(account, cart);

        /*// 订单生成逻辑
        Order order = (Order) session.getAttribute("order");
        if (order == null) {
            order = orderService.initializeOrder(account, cart);
            session.setAttribute("order", order); // 确保存入session
        }*/

        // 如果购物车为空，重定向到购物车页面
        if (cart == null || cart.isEmpty()) {
            return "redirect:/cart/view";
        }

        // 无论会话中是否存在旧订单，都重新生成
        Order order = orderService.initializeOrder(account, cart);
        session.setAttribute("order", order); // 覆盖旧订单

        model.addAttribute("order", order);
        return "order/newOrder";
    }

    // 提交订单
    @PostMapping("/submit")
    public String submitOrder(
            HttpSession session, // 改为从session获取
            RedirectAttributes redirectAttributes) {

        // 从session获取订单对象
        Order order = (Order) session.getAttribute("order");

        try {
            orderService.submitOrder(order);

            // 清空购物车内容，而非仅移除属性
            Cart cart = (Cart) session.getAttribute("cart");
            if (cart != null) {
                cart.clear(); // 添加 clear() 方法到 Cart 类
            }

            // 清除相关session属性
            session.removeAttribute("cart");
            session.removeAttribute("order");
            redirectAttributes.addFlashAttribute("message", "订单提  交成功！");
            return "redirect:/order/results";
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("error", "订单提交失败：" + e.getMessage());
            return "redirect:/order/new";
        }
    }

/*
   // 显示新订单表单
    @GetMapping("/new")
    public String newOrderForm(
            @SessionAttribute Account account,
            @SessionAttribute Cart cart,
            Model model) {

        Order order = orderService.initializeOrder(account, cart);
        model.addAttribute("order", order);
        return "order/newOrder";
    }*/
/*
    // 提交订单
    @PostMapping("/submit")
    public String submitOrder(
            @ModelAttribute Order order,
            HttpSession session,
            RedirectAttributes redirectAttributes) {

        orderService.submitOrder(order);
        session.removeAttribute("cart");
        redirectAttributes.addFlashAttribute("message", "订单提交成功！");
        return  "redirect:/order/results";
    }
    */



}
